/*
=====================================================================

  JSortTable.java
  
  Created by Claude Duguay
  Copyright (c) 2002
  
=====================================================================
*/

package who.tmap.ui.util.jsorttable;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

import who.tmap.ui.util.MiscUIHelper;


public class JSortTable extends JTable implements MouseListener
{
	
  protected int sortedColumnIndex = -1;
  protected boolean sortedColumnAscending = true;
  
  public JSortTable()
  {
    this(new DefaultSortTableModel());
  }
  
  public JSortTable(int rows, int cols)
  {
    this(new DefaultSortTableModel(rows, cols));
  }
  
  public JSortTable(Object[][] data, Object[] names)
  {
    this(new DefaultSortTableModel(data, names));
  }
  
  @SuppressWarnings("unchecked")
  public JSortTable(Vector data, Vector names)
  {
    this(new DefaultSortTableModel(data, names));
  }
  
  public JSortTable(SortTableModel model)
  {
    super(model);
    initSortHeader();
  }

  public JSortTable(SortTableModel model,
    TableColumnModel colModel)
  {
    super(model, colModel);
    initSortHeader();
  }

  public JSortTable(SortTableModel model,
    TableColumnModel colModel,
    ListSelectionModel selModel)
  {
    super(model, colModel, selModel);
    initSortHeader();
  }

  protected void initSortHeader()
  {
    JTableHeader header = getTableHeader();
    header.setDefaultRenderer(new SortHeaderRenderer());
    header.addMouseListener(this);
  }

  public int getSortedColumnIndex()
  {
    return sortedColumnIndex;
  }
  
  public boolean isSortedColumnAscending()
  {
    return sortedColumnAscending;
  }
  
  public void mouseReleased(MouseEvent event)
  {
    TableColumnModel colModel = getColumnModel();
    int index = colModel.getColumnIndexAtX(event.getX());
    int modelIndex = colModel.getColumn(index).getModelIndex();
    
    SortTableModel model = (SortTableModel)getModel();
    if (model.isSortable(modelIndex))
    {
      // toggle ascension, if already sorted
      if (sortedColumnIndex == index)
      {
        sortedColumnAscending = !sortedColumnAscending;
      }
      sortedColumnIndex = index;
    
      model.sortColumn(modelIndex, sortedColumnAscending);
    }
  }
  
  public void mousePressed(MouseEvent event) {}
  public void mouseClicked(MouseEvent event) {}
  public void mouseEntered(MouseEvent event) {}
  public void mouseExited(MouseEvent event) {}
  
  /**
   * Color every other row
   */
  public Component prepareRenderer( TableCellRenderer renderer
          						  , int rowIndex, int vColIndex ) {
	  Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
	  if (rowIndex % 2 == 0 ) {
		  c.setBackground( MiscUIHelper.TABLE_CELL_BG_COLOR );
		  //c.setForeground(Color.white);
	  } else {
		  // If not shaded, match the table's background
		  c.setBackground(getBackground());
		  if ( c.getBackground().equals( c.getForeground() ) ) {
			  c.setForeground( Color.black );
		  }
		  //c.setForeground(Color.black);
	  }
	  return c;
  }
}

